/* global window */
import modelExtend from 'dva-model-extend'
import { config } from 'utils'
import * as deployService from '../services/deploy'
import { pageModel } from './common'
import * as systemProcessService from '../services/systemprocess'
import * as processtypeService from '../services/processtype'

const { fetch } = deployService
const { prefix } = config

export default {
  namespace: 'processconfig',

  state: {
    deploy: {},
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/processconfig') {
          const payload = location.query || { current: 1, pageSize: 10 }
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {
    * query({ payload = {} }, { call, put }) {
      const data = yield call(fetch, payload)
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.list,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.data.total,
            },
          },
        })
      }
    },
  },

  reducers: {
    querySuccess(state, { payload }) {
      const { list, pagination } = payload
      return {
        ...state,
        list,
        pagination: {
          ...state.pagination,
          ...pagination,
        },
      }
    },
  },
}
